package handler

import (
	"github.com/gin-gonic/gin"
	"house/api/requset"
	"house/api/resp"
	"house/internal/dao"
	"house/internal/model"
)

// 查看订单列表
func GetOrderList(c *gin.Context) {
	//接收参数
	var req requset.GetOrderListReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "验证失败", err.Error())
		return
	}

	//业务逻辑
	//权限
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if user.UType != 3 { //3为管理员
		resp.Error(c, 10000, "权限不足", nil)
		return
	}
	var orders []model.Order
	result := dao.GetListByFields(&model.Order{}, &orders)
	if !result {
		resp.Error(c, 10000, "订单查看失败", nil)
		return
	}

	//返回
	resp.Success(c, orders)
}

// 更新订单状态
func UpdateOrderStatus(c *gin.Context) {
	//接收参数
	var req requset.UpdateOrderStatusReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "验证失败", err.Error())
		return
	}

	//业务逻辑
	//权限
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if user.UType != 3 { //3为管理员
		resp.Error(c, 10000, "权限不足", nil)
		return
	}
	var order model.Order
	dao.GetOneById(req.Id, &order)
	if order.Id == 0 {
		resp.Error(c, 10000, "订单不存在", nil)
		return
	}

	result := dao.Update(&model.Order{
		Id:     req.Id,
		Status: req.Status,
	})
	if !result {
		resp.Error(c, 10000, "更新订单状态失败", nil)
		return
	}

	//返回
	resp.Success(c, true)
}
